package com.shinechain.botModel;

import com.shinechain.dao.TelegramUserMapper;
import com.shinechain.model.TelegramUser;
import com.shinechain.utils.PatternChat;
import org.springframework.beans.factory.annotation.Autowired;
import org.telegram.telegrambots.api.methods.groupadministration.GetChatMember;
import org.telegram.telegrambots.api.methods.groupadministration.KickChatMember;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;

import java.util.ArrayList;
import java.util.List;

public class ShineChainChatBot extends TelegramLongPollingBot {

    @Autowired
    TelegramUserMapper telegramUserMapper;

    @Override
    public void onUpdateReceived(Update update) {
        boolean flag = false;
        try{
            flag = !update.getMessage().getNewChatMembers().isEmpty();
            System.out.println("flag"+flag);

        }catch (Exception e){
            e.printStackTrace();
        }
        if (update.hasMessage() && flag) {
            String msg = "我发现这个人:"+update.getMessage().getFrom().getUserName()+"拉了";
            for(int i =0 ; i<update.getMessage().getNewChatMembers().size() ; i++){
                msg+= "id:"+update.getMessage().getNewChatMembers().get(i).getId();
                msg+= "firstname:"+update.getMessage().getNewChatMembers().get(i).getFirstName();

                TelegramUser telegramUser = new TelegramUser();
           ///     telegramUser.setUserid(update.getMessage().getNewChatMembers().get(i).getId());
           //     telegramUser.setIsgroup(1);
           //     telegramUser.setUsername(update.getMessage().getNewChatMembers().get(i).getUserName());
           //     telegramUser.setInviterid(update.getMessage().getFrom().getId());

                telegramUserMapper.insert(telegramUser);
            }
            msg += "进群。";
            System.out.println(msg);
            SendMessage message = new SendMessage()
                    .setChatId(update.getMessage().getChatId())
                    .setText(msg);
       /*     try {
                execute(message); // Call method to send the message
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }*/
        }
          if (update.hasMessage() && update.getMessage().hasText()) {
           System.out.println(update.getMessage().getText());
           //ad Change !!!!
            if(PatternChat.patternChat(update.getMessage().getText()).isEmpty()){
                SendMessage message = new SendMessage() // Create a SendMessage object with mandatory fields
                        .setChatId(update.getMessage().getChatId())
                        .setText("你骂人QAQ。。"+update.getMessage().getFrom().getUserName()+"我要T了你");
                KickChatMember kickChatMember = new KickChatMember();
                kickChatMember.setChatId(update.getMessage().getChatId());
                System.out.println(update.getMessage().getFrom().getUserName());
                System.out.println(update.getMessage().getChatId());
                kickChatMember.setUserId(update.getMessage().getFrom().getId());
                TelegramUser telegramUser = new TelegramUser();
         //       telegramUser.setUserid(update.getMessage().getFrom().getId());
          //      telegramUser.setIsgroup(0);

                try {
                    execute(message); // Call method to send the message
                    execute(kickChatMember);
                    telegramUserMapper.updateByPrimaryKeySelective(telegramUser);

                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    @Override
    public String getBotUsername() {
        return "leek_bot";
    }

    @Override
    public String getBotToken() {
        return "634777415:AAFmTHfxYspSOudhzhsqmfajH37VoUla-IU";
    }


    public ShineChainChatBot(DefaultBotOptions options) {
        super(options);
    }

    public void sendMessage(String message){
        SendMessage sendMessage = new SendMessage();
        sendMessage.setChatId("-1001389649632");
        sendMessage.setText(message);

        try {
            execute(sendMessage); // Call method to send the message
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }

    }

    public String checkChannel(String channelId,int userId){
        GetChatMember getChatMember = new GetChatMember();
        getChatMember.setUserId(userId);
        getChatMember.setChatId(channelId);

        try {
            String res = execute(getChatMember).getStatus(); // Call method to send the message
            System.out.println("Status="+res);
            return  res;



        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
        return "false";

    }

    public String sendKeyBoard(String chatId){
        ReplyKeyboardMarkup replyKeyboardMarkup=new ReplyKeyboardMarkup();
        List<KeyboardRow> rows=new ArrayList<>();
        KeyboardRow row=new KeyboardRow();
        KeyboardButton keyboardButton=new KeyboardButton("Button 1");
        row.add(keyboardButton);
        rows.add(row);
        replyKeyboardMarkup.setKeyboard(rows);
        replyKeyboardMarkup.setOneTimeKeyboard(false);
        SendMessage message = new SendMessage() ;
        message
                .setChatId(chatId)
                .setText("Configurando Keyboard")
                .setReplyMarkup(replyKeyboardMarkup);
        try {
           execute(message);
            return  "true";
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
        return "false";

    }


}
